package com.aos.erp.fs.web;

import com.aos.erp.common.GlobalConstant;
import com.aos.erp.common.utils.NumUtil;
import com.aos.erp.fs.model.SysFile;
import com.aos.erp.fs.service.SysFileService;
import com.aos.erp.fs.show.FileViewResolver;
import com.aos.erp.fs.show.ViewType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Controller()
@Api("文件浏览")
public class SysFileShowController {

    @Resource
    private SysFileService fileService;
    @Resource
    private FileViewResolver fileViewResolver;

    @GetMapping(path = {"/file/show",GlobalConstant.GATEWAY_IGNORE+"/file/show"})
    @ApiOperation(value = "文件浏览，查看")
    @ApiImplicitParam(name = "id", required = true,paramType = "query")
    public void showById(HttpServletRequest request, HttpServletResponse response, String id) {

        long fileId = NumUtil.toLong(id);
        SysFile sysFile = fileService.getById(fileId);

        if (sysFile != null) {
            fileViewResolver.show(request, response, sysFile, ViewType.open);
        }
    }

    @GetMapping("/file/download")
    @ApiOperation(value = "文件下载")
    @ApiImplicitParam(name = "id", required = true,paramType = "query")
    public void downloadById(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") String id) {

        long fileId = NumUtil.toLong(id);
        SysFile sysFile = fileService.getById(fileId);

        if (sysFile != null) {
            fileViewResolver.show(request, response, sysFile, ViewType.download);
        }
    }
}
